package com.solution.admin.coupon.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.coupon.entity.CouponGetEntity;
import com.solution.admin.coupon.service.CouponGetService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 优惠券领用信息web控制层
 * @author xu_cc
 * @date 2017-11-16 11:11:57
 */
@RequestMapping("/couponGet")
@Controller
public class CouponGetController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(CouponGetController.class);

    @Autowired
    private CouponGetService couponGetService;
    
    /**
     * 跳转优惠券领用信息查询页面
     * @param CouponGet 接收界面查询条件参数
     * @param request 请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
      * @date 2017-11-16 11:11:57
     */
    @RequestMapping("/toListCouponGetPage")
    public String toListCouponGetPage(CouponGetEntity couponGet, HttpServletRequest request) {
        return "coupon/couponGet_list";
    }

    /**
     * 分页查询优惠券领用信息列表
     * @param couponGet 接收界面查询条件参数
     * @param request 请求对象
     * @return 返回对应的页面视图
     * @author xu_cc
      * @date 2017-11-16 11:11:57
     */
    @RequestMapping("/listCouponGetPage")
    @ResponseBody
    public JSONGrid listCouponGetPage(CouponGetEntity couponGet, HttpServletRequest request){
        int pageNum=Integer.parseInt(request.getParameter("page").toString());
        int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
        PageParam pageParam = super.getPageParam(request);
        pageParam.setPageNum(pageNum);
        pageParam.setNumPerPage(numPerPage);
        
        
        
        Map<String, Object> paramMap = new HashMap<String, Object>();
        try {
            PageBean pageBean = couponGetService.listCouponGetPage(pageParam, paramMap);
            return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
        } catch (Exception e) {
            handleException("分页查询优惠券领用信息失败", logger, e);
        }
        return new JSONGrid();
    }
    
    
    /**
     * 跳转新增页面
     * @return string 跳转页面路径
     * @author xu_cc
      * @date 2017-11-16 11:11:57
     */
    @RequestMapping("/toAddCouponGet")
    public String toAddCouponGet(HttpServletRequest request) {
        request.setAttribute("opFlag", "add");
        return "coupon/couponGet_form";
    }

    /**
     * 跳转编辑页面
     * @return string 跳转页面路径
     * @author xu_cc
      * @date 2017-11-16 11:11:57
     */
    @RequestMapping("/toUpdateCouponGet")
    public String toUpdateCouponGet(HttpServletRequest request) {
        // 编辑操作
        request.setAttribute("opFlag", "edit");
        long id = Long.valueOf(request.getParameter("id"));
        try {
            CouponGetEntity couponGet = couponGetService.getCouponGetById(id);
            request.setAttribute("couponGet", couponGet);
        } catch (Exception e) {
            handleException("跳转编辑优惠券领用信息页面错误", logger, e);
        }
        return "coupon/couponGet_form";
    }


    /**
     * 新增或保存优惠券领用信息
     * @param request 请求对象
     * @param couponGetEntity 优惠券领用信息实体
     * @return
     * @author xu_cc
      * @date 2017-11-16 11:11:57
     */
    @RequestMapping("/saveOrUpdateCouponGet")
    @ResponseBody
    public BaseReturn saveCouponGet(HttpServletRequest request, CouponGetEntity couponGetEntity) {
        BaseReturn result = new BaseReturn();
        try {
            String id = request.getParameter("id");
            if(StringUtil.isNotEmpty(id)){
                couponGetService.updateCouponGet(couponGetEntity);
            }else{
                couponGetService.saveCouponGet(couponGetEntity);
            }
            result.setCode(com.solution.common.constants.PublicStatus.SysCode.SUCCESS);
            result.setMsg("优惠券领用信息保存成功！");
        } catch (Exception e) {
            result.setCode(com.solution.common.constants.PublicStatus.SysCode.ERROR);
            result.setMsg("优惠券领用信息保存异常！");
        }
        return result;
    }
    
    /**
     * 删除用户的优惠券
     * @param request
     * @return
     * @author xkl
     * @date 2017年12月18日 下午5:21:48
     */
    @RequestMapping("/deleteCouponGet")
    @ResponseBody
    public BaseReturn deleteCouponGet(HttpServletRequest request) {
		long id = Long.valueOf(request.getParameter("id"));
		long couponId = Long.valueOf(request.getParameter("couponId"));
        BaseReturn result = new BaseReturn();
		try {
            couponGetService.deleteCouponGetById(id, couponId);
            result.setCode(PublicStatus.SysCode.SUCCESS);
            result.setMsg("删除用户优惠券成功！");
		} catch (Exception e) {
			e.printStackTrace();
            result.setCode(PublicStatus.SysCode.ERROR);
            result.setMsg("删除用户优惠券异常！");
		}
        return result;
    }
    
}
